package cn.tedu.store.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.tedu.store.entity.Address;
import cn.tedu.store.entity.Order;
import cn.tedu.store.entity.OrderItem;
import cn.tedu.store.mapper.OrderMapper;
import cn.tedu.store.service.IAddressService;
import cn.tedu.store.service.ICartService;
import cn.tedu.store.service.IOrderService;
import cn.tedu.store.service.ex.AccessDeniedException;
import cn.tedu.store.service.ex.AddressNotFoundException;
import cn.tedu.store.service.ex.InsertException;
import cn.tedu.store.vo.CartVO;
@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private IAddressService addressService; //需要根据收货地址aid查询数据
	
	@Autowired
	private ICartService cartService; //需要根据若干个购物车的id查询数据
	
	@Override
	@Transactional
	public void create(Integer aid, Integer[] cids, Integer uid, String username)
			throws AddressNotFoundException, AccessDeniedException, InsertException {
		//创建当前时间对象
		Date now = new Date();
		
		// 根据参数aid调用addressService的findByAid(aid)查询数据
		Address result = addressService.getByAid(aid);
		// 判断查询结果是否为null：AddressNotFoundException
		if(result==null) {
			throw new AddressNotFoundException("地址未找到");
		}
		// 判断查询结果的数据归属是否错误：AccessDeniedException
		if(result.getUid()!=uid) {
			throw new AccessDeniedException("数据归属异常");
		}
		// 得到：name, phone, address,district
		String name = result.getName();
		String phone = result.getPhone();
		String district = result.getDistrict();
		String address = result.getAddress();

		// 根据参数cids调用cartService中getByCids(cids, uid)查询数据，得到List<CartVO>，即carts
		List<CartVO> carts = cartService.getByCids(cids, uid);

		// 定义总价变量totalPrice=0
		Double totalPrice=0.0;
		// 遍历carts
		for (CartVO cart : carts) {
			// 计算每种商品的价格：price * num，并累加到totalPrice
			totalPrice += cart.getPrice()*cart.getNum();
		}
		

		// 创建订单对象：new Order()
		Order order = new Order();
		// 向订单对象中封装数据：uid
		order.setUid(uid);
		// 向订单对象中封装数据：name, phone, address
		order.setName(name);
		order.setPhone(phone);
		order.setAddress(district+address);
		// 向订单对象中封装数据：total_price
		order.setTotalPrice(totalPrice);
		// 向订单对象中封装数据：state=0, order_time=now, pay_time=null
		order.setState(0);
		order.setOrderTime(now);
		order.setPayTime(null);
		// 向订单对象中封装数据：4个日志
		order.setCreatedTime(now);
		order.setCreatedUser(username);
		order.setModefiedTime(now);
		order.setModefiedUser(username);
		// 插入订单数据：insertOrder(order)
		insertOrder(order);
	
		// 从插入时的参数order中获取oid
		int oid = order.getOid();
		
		// 遍历carts,插入订单商品数据
		for (CartVO cart : carts) {
			// -- 创建订单商品对象
			OrderItem orderItem = new OrderItem();
			// -- 向订单对象中封装数据：oid
			orderItem.setOid(oid);
			// -- 向订单对象中封装数据：title, image, price, num
			orderItem.setTitle(cart.getTitle());
			orderItem.setImage(cart.getImage());
			orderItem.setPrice(cart.getPrice());
			orderItem.setNum(cart.getNum());
			// -- 向订单对象中封装数据：4个日志
			orderItem.setCreatedTime(now);
			orderItem.setCreatedUser(username);
			orderItem.setModefiedTime(now);
			orderItem.setModefiedUser(username);
			// -- 插入订单商品数据：insertOrderItem(orderItem)
			insertOrderItem(orderItem);
			
		}
		
		
		
		
		

	}

	/**
	 * 私有化持久层方法
	 */
	
	private void insertOrder(Order order) {
		Integer rows = orderMapper.insertOrder(order);
		if(rows!=1) {
			throw new InsertException("添加订单失败，插入出现未知异常");
		}
	};
	
	private void insertOrderItem(OrderItem orderItem) {
		Integer rows = orderMapper.insertOrderItem(orderItem);
		if(rows!=1) {
			throw new InsertException("添加订单商品失败，出现未知异常");
		}
	}
	
	
}
